﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Routing;
using TiTi.tea.Api.Attributes;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Comm;
using TiTi.tea.Model;
using TiTi.tea.Dto;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/Location")]
    public class LocationController : ApiController
    {
        private LocationBLL bll = new LocationBLL();

        /// <summary>
        /// 获取用户地址
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("GetByUser")]
        [HttpGet]
        public Result Get(int userId)
        {
            var data = bll.Get(userId);
            return Result.Success(data);
        }

        /// <summary>
        /// 获取用户默认地址
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("GetDefault")]
        [HttpGet]
        public Result GetDefault(int userId)
        {
            var data = bll.GetDefault(userId);
            if(data==null) return Result.Success(null);
            return Result.Success(new AddressDto(data));
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        [MyAuthorize]
        public Result Add(LocationVM param)
        {
            IHttpRouteData routeData = Request.GetRouteData();
            IDictionary<string, object> dict = routeData.Values;
            var auth = dict["auth"] as AuthInfo;
            var userId = auth.UserId;
            
            if (bll.Add(new LocationInfo()
            {
                Contacts = param.name,
                Sex = param.gender,
                Tel = param.phone,
                Location = param.address,
                LocationDetail = param.description,
                Latitude = param.latitude,
                Longitude=param.longitude,
                UserId= userId,
                
            }))

            {
                return Result.Success();
            }
            else
            {
                return Result.Error("添加失败");
            }
            return Result.Error("添加失败");

        }

        /// <summary>
        /// 查询一个
        /// </summary>
        /// <returns></returns>
        [Route("GetOne")]
        [HttpGet]
        public Result GetOne(int id)
        {
            var data = bll.GetOne(id);
            return Result.Success(new AddressDto()
            {
                id = data.Id,
                userId = data.UserId,
                contacts = data.Contacts,
                Sex = data.Sex,
                tel = data.Tel,
                location = data.Location,
                locationDetail = data.LocationDetail,
                isDefault = data.IsDefault,
                latitude = data.Latitude,
                longitude = data.Longitude
            });
        }

        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update")]
        [HttpPut]
        public Result Update(LocationInfo param)
        {
            if (param.Id == 0)
            {
                return Result.Error("Id不能为空");
            }
            else
            {
                LocationInfo lo = bll.GetOne(param.Id);
                lo.UserId = param.UserId;
                lo.Contacts = param.Contacts;
                lo.Sex = param.Sex;
                lo.Tel = param.Tel;
                lo.Location = param.Location;
                lo.LocationDetail = param.LocationDetail;
                lo.IsDefault = param.IsDefault;
                bool num = bll.Update(lo);
                if (num)
                {
                    return Result.Success();
                }
                else
                {
                    return Result.Error("修改失败");
                }
            }
        }
    }
}
